﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using RoleUpdater.DelegatesHelpers;

namespace RoleUpdater.Controls
{
    public partial class UpdateControl : UserControl
    {
        private readonly RoleManager _manager;
        private readonly UpdateSettings _settings;

        public UpdateControl(RoleManager manager, UpdateSettings settings)
        {
            InitializeComponent();

            _manager = manager;
            _settings = settings;
        }

        private void UpdateControlLoad(object sender, EventArgs e)
        {
            var bw = new BackgroundWorker();
            bw.RunWorkerCompleted += BwRunWorkerCompleted;
            bw.ProgressChanged += BwProgressChanged;
            bw.DoWork += BwDoWork;
            bw.RunWorkerAsync();
        }

        void BwDoWork(object sender, DoWorkEventArgs e)
        {
            foreach (var role in _settings.SelectedRoles)
            {
                try
                {
                    LabelDelegates.SetText(lblProgress, "Retrieving privileges set for role " + role["name"] + "...");

                    var rolePrivileges = _manager.GetPrivilegesForRole(role.Id);

                    foreach (var pAction in _settings.Actions)
                    {
                        if (pAction.Level == PrivilegeLevel.None)
                        {
                            LabelDelegates.SetText(lblProgress,
                                                   "Removing privilege " + pAction.PrivilegeName + " from role " +
                                                   role["name"] + "...");
                            _manager.RemovePrivilegeFromRole(rolePrivileges, pAction.PrivilegeId);
                        }
                        else
                        {
                            LabelDelegates.SetText(lblProgress,
                                                   "Adding/Updating privilege " + pAction.PrivilegeName + " in role " +
                                                   role["name"] + "...");
                            _manager.AddPrivilegeToRole(rolePrivileges, pAction);
                        }
                    }

                    LabelDelegates.SetText(lblProgress, "Replacing privileges set for role " + role["name"] + "...");
                    _manager.ApplyChanges(rolePrivileges, role.Id);
                }
                catch (Exception error)
                {
                    CommonDelegates.DisplayMessageBox(ParentForm,
                                                      "Error while updating role " + role["name"] + ": " + error.Message,
                                                      "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        void BwProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            //throw new NotImplementedException();
        }

        void BwRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            LabelDelegates.SetText(lblProgress, "Update done!");
        }


    }
}
